class Solution {
    int row, col;

    void dfs(vector<vector<char>>& grid, int m, int n)
    {
        if (m < 0 || n < 0 || m >= row || n >= col || grid[m][n] != '1')
            return;

        grid[m][n] = '2';
        dfs(grid, m - 1, n);    //上
        dfs(grid, m, n - 1);    //左
        dfs(grid, m + 1, n);    //下
        dfs(grid, m, n + 1);    //右
    }

public:
    int numIslands(vector<vector<char>>& grid) {
        int ret = 0;
        row = grid.size();
        col = grid[0].size();
        for (int i = 0; i < row; i++)
            for (int j = 0; j < col; j++)
                if (grid[i][j] == '1')
                {
                    dfs(grid, i, j);
                    ret++;
                }

        return ret;
    }
};